/*
 *  testasm.c - little testprogram for compressor and ARM assembler decompressor
 *
 *  Written by:
 *   Andreas Dehmel
 *
 *  This file is part of armDeu, the portable WAD utility (the name derives
 *  from its initial port to the ARM-based RISC OS). armDeu is released under
 *  the GNU Public License (GPL) in the hope that it proves useful. Please
 *  note there is NO WARRANTY. For more information read the file License
 *  included in this release.
 */


#include <stdio.h>
#include <stdlib.h>

#include <fastlz.h>



#define BUFFERSIZE	256*1024


#define LOOPNUMBER	10




extern unsigned int ReadMonotonicTime(void);





static char buffer[BUFFERSIZE];




int main(int argc, char *argv[])
{
  FILE *file;
  int compSize;
  int outSize = 576243; /* horrible, yes ;-) */
  char *outBuff;
  int status;
  unsigned int time0, time1, iotime;
  int loop;
  fastlz_decompfile_context_t decompCtx;

  fastlz_decompress_init(&decompCtx.basectx);
  decompCtx.basectx.ioBuffSize = 0x10000;
  decompCtx.basectx.fastlz_refill = fastlz_file_refill;

  file = fopen("compdata", "rb");
  fread(&compSize, 1, 4, file);
  outBuff = malloc(outSize);

  printf("in: %p, out: %p\n", buffer, outBuff);

  time0 = ReadMonotonicTime();

  if (compSize <= BUFFERSIZE)
  {
    for (loop=0; loop<LOOPNUMBER; loop++)
    {
      fseek(file, 4, SEEK_SET);
      fread(buffer, 1, compSize, file);
    }
  }

  time1 = ReadMonotonicTime();
  iotime = time1 - time0;
  time0 = time1;

  for (loop=0; loop<LOOPNUMBER; loop++)
  {
    fseek(file, 0, SEEK_SET);
    status = fastlz_decompress_block(&decompCtx.basectx, outBuff, outSize);
  }

  time1 = ReadMonotonicTime();

  printf("Status: %d, time: %d, rate = %d bps\n", status, time1-time0, (100*LOOPNUMBER*outSize)/(time1 - time0));

  if (compSize <= BUFFERSIZE)
  {
    printf("Subtracting IO: time %d, rate %d bps\n", time1-time0-iotime, (100*LOOPNUMBER*outSize)/(time1-time0-iotime));
  }

  fclose(file);

  file = fopen("uncompasm", "wb");
  fwrite(outBuff, 1, outSize, file);
  fclose(file);

  return 0;
}
